package zhu.areas.team.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import zhu.areas.team.data.Team;
import zhu.areas.team.data.TeamMembers;
import zhu.areas.team.data.TeamNotice;
import zhu.common.dao.BaseDAOImpl;

public class TeamZhDAOImpl extends BaseDAOImpl implements ITeamZhDAO{
	
	
	public Team findByProperty(String propertyName, Object value){
		try{
		return (Team) super.findByPropertyUnique(propertyName, value, "Team");
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw e;
		}
	}

	public List<TeamMembers> findMeByTeamId(final String propertyName, final Object value){
		final String instanceType="TeamMembers";
		try{
			return  (List<TeamMembers>)getHibernateTemplate().execute(
					new HibernateCallback() {
						public Object doInHibernate(final Session session)
								throws HibernateException, SQLException {
							final String hql = "from "+ instanceType+"  where  " + propertyName + " = '"
									+ value + "'";
							final Query query = session.createQuery(hql);
							return query.list();
						}
					});
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw e;
		}
		
		
		
	}
	public TeamNotice findTopNotice(final String propertyName,final Object value){
		final String instanceType="TeamNotice";
		try{
			return  (TeamNotice)getHibernateTemplate().execute(
					new HibernateCallback() {
						public Object doInHibernate(final Session session)
								throws HibernateException, SQLException {
							final String hql = "from "+ instanceType+"  where  " + propertyName + " = '"
									+ value + "'"+ "' order by teamPubTime desc";
							final Query query = session.createQuery(hql);
							query.setMaxResults(1);
							return query.uniqueResult();
						}
					});
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw e;
		}
		
	}

	public void delete(Team team){
		try {
			super.delete(team);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw e;
		}
	}
	public void saveOrUpdateteaminfo(Team team){
		try {
			super.saveOrUpdate(team);
			
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw e;
		}
		
	}
}
